var _baseUrl = "";
var _mainContainer = '.bodyMain';
// Implement selector
_ = function(e) {
	var me = this;
	if (e[0] == '.') {
		return document.getElementsByClassName(e.substr(1));
	} else if (e[0] == '#') {
		return document.getElementById(e.substr(1));
	} else {
		return document.getElementsByTagName(e);
	}
};

_.data = {
	baseRequestUrl : _baseUrl,
	beforeRequest : null,
	afterRequest : function(){$.unblockUI();},
	maxRetryRequest : 2,
	isDebugMode : true,
	xmlhttp : null,
	isAsync : true,
	request : function(controller, action, data, callBackSuccess, callBackError) {
		var retryCount = 0;
		var url = _.data.baseRequestUrl + "/" + controller;
		if (_.data.beforeRequest) {
			_.data.beforeRequest();
		}
		if (_.data.xmlhttp === null) {
			if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome,
				// Opera, Safari
				_.data.xmlhttp = new XMLHttpRequest();
			} else {// code for IE6, IE5
				_.data.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		if (_.data.isAsync) {
			_.data.xmlhttp.open("POST", url, true);
			_.data.xmlhttp.onreadystatechange = function() {
				_.data.receiveResponse(_.data.xmlhttp, callBackSuccess,
						callBackError);
			};
		} else {
			_.data.xmlhttp.open("POST", url, false);
		}

		_.data.xmlhttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded");
		_.data.xmlhttp.setRequestHeader("charset", "UTF-8");
		_.data.xmlhttp.setRequestHeader("action", action);
		// Clear <data> tab if exist
		if (typeof (data) =='string') {
			while (data.search('<data>') > -1) {
				data = data.replace('<data>', '');
			}
			while (data.search('</data>') > -1) {
				data = data.replace('</data>', '');
			}
		}
		_.data.xmlhttp.setRequestHeader("data", JSON.stringify('<data>' + data
				+ '</data>'));
		_.data.xmlhttp.send("data="
				+ JSON.stringify('<data>' + data + '</data>'));
		if (!_.data.isAsync) {
			_.data.receiveResponse(_.data.xmlhttp, callBackSuccess,
					callBackError);
		}
		$.blockUI({ css: { backgroundColor: 'none', color: '#fff',border:"none",textAlign:"center", zIndex:10000,paddingLeft:"14%" },message: '<div class="bubblingG"><span id="bubblingG_1"> </span> <span id="bubblingG_2"> </span> <span id="bubblingG_3"> </span>	</div>' });
	},
	receiveResponse : function(xmlhttp, callBackSuccess, callBackError) {
		try {
			// has been set up/has been sent/is in process
			if (xmlhttp.readyState == 1 || xmlhttp.readyState == 2
					|| xmlhttp.readyState == 3) {
				return;
			}
			if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
				if (_.data.afterRequest) {
					_.data.afterRequest();
				}
				callBackSuccess(xmlhttp.responseText);
			} else // On Error
			if (xmlhttp.status === 404) {
				if (_.data.afterRequest) {
					_.data.afterRequest();
				}
				if (_.data.isDebugMode) {
					callBackError("This controller is not exist"
							+ xmlhttp.responseText);
				} else {
					callBackError("Sorry! Too much request now, please try later");
				}
			} else // On Error
			{
				if (_.data.afterRequest) {
					_.data.afterRequest();
				}
				if (_.data.isDebugMode) {
					callBackError(xmlhttp.respontText);
					// alert(xmlhttp.respontText);
				} else {
					console.log(xmlhttp.respontText);
				}
			}
		} catch (ex) {
			if (_.data.afterRequest) {
				_.data.afterRequest();
			}
			if (_.data.isDebugMode) {
				callBackError(ex);
				// alert(ex);
			} else {
				console.log(ex);
			}
		}
	}
};

// Document Ready Event;
_.DocumentReady = function() {
};
// Document Ready Event Register
_.ready = function(callback) {
	_.DocumentReady = callback;
};
document.onreadystatechange = function(e) {
	if (document.readyState = 'complete') {
		if (typeof (_.DocumentReady) == 'function') {
			_.DocumentReady();
			localStorage.clear();
			localStorage.setItem('currentDate',new Date());
			localStorage.setItem('currentViewMode','date');
			mybudget.router.todayTransaction();
			mybudget.router.init();
		}
	}
};
$(document).ajaxStop($.unblockUI); 